package com.crm.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Transaction;

import com.crm.dao.base.BaseHibernateDAO;
import com.crm.po.Users;

public class UserDAO extends BaseHibernateDAO {
	
	private static Logger logger = Logger.getLogger(UserDAO.class);
	
	public void save(Users users) {
		if(users.getUid() == null || users.getUid().trim().length() == 0){
			users.setUid(this.getUUID());
		}
		Transaction transaction = getSession().beginTransaction();
		try {						
			this.getSession().save(users);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}

	public void saveOrUpdate(Users users) {
		if(users.getUid() == null || users.getUid().trim().length() == 0){
			users.setUid(this.getUUID());
		}
		Transaction transaction = getSession().beginTransaction();
		try {						
			this.getSession().saveOrUpdate(users);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}
	
	public void delete(Users users) {
		Transaction transaction = getSession().beginTransaction();
		try {
			this.getSession().delete(users);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}

	public Users findById(String id) {
		Users users = null;
		try {
			users = (Users) this.getSession().get(Users.class, id);
		} catch (RuntimeException e) {
			logger.error(e);
		} finally {
			this.closeSession();	
		}
		return users;
	}
	
	public Users findUser(String hql) {
		return (Users) getUniqueResult(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<Users> usersList(String hql) {
		return (List<Users>) findList(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<Users> usersListPage(String hql, int start, int limit) {
		return (List<Users>) findListPage(hql, start, limit);
	}


}